//
//  MyPostViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "MyPostViewController.h"
#import "ForumDetailViewController.h"

#import "ForumCell.h"
@interface MyPostViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSMutableArray *dataArr;//数据
@property (nonatomic,assign)NSInteger pageNumber;//页码
@end

@implementation MyPostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    [self getMyPosts];
}

-(void)creatView
{
    [self initTitleWithTitle:@"我的帖子"];
    _dataArr = [NSMutableArray new];
    _pageNumber = 1;
    self.view.backgroundColor = backColor;
    [self.view addSubview:self.tableV];
}

-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStyleGrouped];
        _tableV.estimatedRowHeight = 60*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
        //刷新
        _tableV.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            _pageNumber = 1;
            [_dataArr removeAllObjects];
            [_tableV.mj_footer resetNoMoreData];
            [self getMyPosts];
        }];
        //加载
        _tableV.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
            _pageNumber ++;
            [self getMyPosts];
        }];
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5*screenW_6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //加载cell
    ForumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ForumCell"];
    if (cell == nil) {
        cell = [[ForumCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ForumCell"];
    }
    [cell loadCellWithData:_dataArr[indexPath.section]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat h;
    h += [TomamaUI getStringHeightWith:_dataArr[indexPath.section][@"content"] andWeith:SCREENW - 30*screenW_6 andFont:[UIFont systemFontOfSize:14*screenW_6]];
    NSArray *imags = _dataArr[indexPath.section][@"imgList"];
    if (imags.count) {
        h += 175*screenW_6;
    }
    return h + 110*screenW_6;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataArr.count < indexPath.section) {
        return;
    }
    __weak typeof(self) weakSelf = self;
    ForumDetailViewController *vc = [ForumDetailViewController new];
    vc.postId = _dataArr[indexPath.section][@"id"];
    vc.deleteBlcok = ^(NSString *postId){
        [weakSelf.dataArr removeObjectAtIndex:indexPath.row];
        [tableView reloadData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)getMyPosts
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/post/postList" Dic:@{@"jdUserId":[Storage getToken],@"userId":[Storage getToken],@"pageNo":[ NSString stringWithFormat:@"%ld",_pageNumber],@"pageSize":@"10"} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    if ([responseDic[@"count"] integerValue] > _dataArr.count) {
                        [_dataArr addObjectsFromArray:responseDic[@"data"]];
                    }
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
                [_tableV reloadData];
                if ([_tableV.mj_header isRefreshing]) {
                    [_tableV.mj_header endRefreshing];
                }
                if ([_tableV.mj_footer isRefreshing]) {
                    if ([responseDic[@"count"] integerValue] > _dataArr.count) {
                        [_tableV.mj_footer endRefreshing];
                    }else{
                        [_tableV.mj_footer endRefreshingWithNoMoreData];
                    }
                }
            });
        }];
    });
}

@end
